//
//  RHBatteryCellVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/27.
//

#import "RHBatteryCellVC.h"
#import "RHBatteryCellItemCell1.h"
#import "RHBatteryCellItemCell2.h"
#import "RHDevPackModel.h"
#import "RHBatteryCellFullChartVC.h"

@interface RHBatteryCellVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectView;

@property (nonatomic, strong) NSArray *packDataArr;

@property (nonatomic, assign) NSInteger packIndex;

@property (nonatomic, strong) RHDevPackModel *lastModel;
@end

@implementation RHBatteryCellVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getPackInfoWithMaster:self.masterSN];
    [self setupUI];
}

-(void)setupUI
{
    UIView *topView = kView;
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_offset(0);
        make.height.mas_offset(30);
    }];
    UIView *normalPoint = kView;
    normalPoint.backgroundColor = kRGB(57, 222, 63);
    normalPoint.layer.cornerRadius = 5;
    [topView addSubview:normalPoint];
    [normalPoint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(10, 10)));
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *norLab = [QMUILabel labelWithText:@"正常电芯".rh_localized font:kSYSTEMFONT(12) textColor:kColorRGBA(63, 68, 67, 0.6)];
    [topView addSubview:norLab];
    [norLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(normalPoint.mas_right).offset(15);
        make.centerY.equalTo(normalPoint);
    }];
    
    UIView *faultPoint = kView;
    faultPoint.backgroundColor = kRGB(255, 108, 97);
    faultPoint.layer.cornerRadius = 5;
    [topView addSubview:faultPoint];
    [faultPoint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(10, 10)));
        make.left.equalTo(norLab.mas_right).offset(25);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *faultLab = [QMUILabel labelWithText:@"高温电芯".rh_localized font:kSYSTEMFONT(12) textColor:kColorRGBA(63, 68, 67, 0.6)];
    [topView addSubview:faultLab];
    [faultLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(faultPoint.mas_right).offset(15);
        make.centerY.equalTo(normalPoint);
    }];
    
    UIView *lowPoint = kView;
    lowPoint.backgroundColor = kRGB(38, 111, 232);
    lowPoint.layer.cornerRadius = 5;
    [topView addSubview:lowPoint];
    [lowPoint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(10, 10)));
        make.left.equalTo(faultLab.mas_right).offset(25);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *lowLab = [QMUILabel labelWithText:@"低温电芯".rh_localized font:kSYSTEMFONT(12) textColor:kColorRGBA(63, 68, 67, 0.6)];
    [topView addSubview:lowLab];
    [lowLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lowPoint.mas_right).offset(15);
        make.centerY.equalTo(lowPoint);
    }];
 
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectView.backgroundColor = CFTabViewBgColor;
    _collectView.showsVerticalScrollIndicator = NO;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [_collectView registerClass:[RHBatteryCellItemCell1 class] forCellWithReuseIdentifier:@"RHBatteryCellItemCell1"];
    [_collectView registerClass:[RHBatteryCellItemCell2 class] forCellWithReuseIdentifier:@"RHBatteryCellItemCell2"];
    [self.view addSubview:_collectView];
    
    
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom);
        make.left.right.bottom.mas_offset(0);
    }];
    kWeakSelf;
    [_collectView addHeaderRefresh:^{
        [weakSelf getPackInfoWithMaster:weakSelf.masterSN];
    }];
}


//根据master获取 pack和电芯数据
-(void)getPackInfoWithMaster:(NSString*)masterSN
{
    [QMUITips showLoadingInView:self.view];
    NSDictionary *para = @{@"deviceId":ValidNullData(self.devModel.deviceId),
                           @"masterSn":ValidNullData(masterSN)};
    [RHDevPackModel startGetWithCompletionAPIKey:H_getPackCellData argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
          
            self.packDataArr = resultModel.data;
            self.packIndex = 0;
            [self.packDataArr enumerateObjectsUsingBlock:^(RHDevPackModel  *_Nonnull packModel, NSUInteger idx, BOOL * _Nonnull stop) {
                NSMutableArray *cellArr = [NSMutableArray array];
                [packModel.volt enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    RHDevCellModel *cellModel = [[RHDevCellModel alloc]init];
                    cellModel.volt = [NSString stringWithFormat:@"%.1f",[ValidNullData(obj) floatValue]];
                    if(idx<packModel.temp.count){
                        cellModel.temp = [NSString stringWithFormat:@"%.1f",[ValidNullData(packModel.temp[idx]) floatValue]];
                    }
                    [cellArr addObject:cellModel];
                }];
                packModel.cells = cellArr.copy;
            }];
            [self.collectView reloadData];

        }
        [self.collectView endHeaderRefresh];
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.collectView endHeaderRefresh];
    }];
        
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    RHDevPackModel *packModel = self.packDataArr[self.packIndex];
    if(section == 0){
        return self.packDataArr.count;
    }else{
        return packModel.cells.count;
    }

}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        RHBatteryCellItemCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHBatteryCellItemCell1" forIndexPath:indexPath];
        RHDevPackModel *packModel = self.packDataArr[indexPath.item];
        cell.packModel = packModel;
        cell.packNoLab.text = [NSString stringWithFormat:@"PACK %ld#",indexPath.item+1];
        return cell;
    }else{
        RHBatteryCellItemCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHBatteryCellItemCell2" forIndexPath:indexPath];
        RHDevPackModel *packModel = self.packDataArr[self.packIndex];
        cell.cellModel = packModel.cells[indexPath.item];
        cell.packNoLab.text = [NSString stringWithFormat:@"%@ %ld",@"电芯".rh_localized,indexPath.item+1];
        kWeakSelf;
        cell.chartBlock = ^{
            RHBatteryCellFullChartVC *loadVC = [[RHBatteryCellFullChartVC alloc]initWithDeviceModel:weakSelf.devModel packSN:packModel.sn cellIndex:indexPath.item + 1];
            loadVC.title = [NSString stringWithFormat:@"%ld#%@",indexPath.item+1,@"电芯".rh_localized];
           
            
            [weakSelf.navigationController pushViewController:loadVC animated:NO];
        };
        return cell;
    }
    
   
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0){
        CGFloat w = (kUI_WINDOW_W -30-20)/3;
        CGFloat h = 75;
        return CGSizeMake(w, h);
    }else{
        CGFloat w = (kUI_WINDOW_W -45)/2;
        CGFloat h = 110;
        return CGSizeMake(w, h);
    }
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(15, 15, 15, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if(section == 0){
        return 10;
    }
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if(section == 0){
        return 10;
    }
    return 15;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        
        RHDevPackModel *packModel = self.packDataArr[indexPath.item];
        NSLog(@"--->当前sn:%@",packModel.sn);
        NSLog(@"--->上一sn:%@",self.lastModel.sn);
        if(![packModel.sn isEqualToString:self.lastModel.sn]){
            self.packIndex = indexPath.item;
            packModel.isSelected = YES;
            self.lastModel.isSelected = NO;
            self.lastModel = packModel;
            [self.collectView reloadData];
        }
        
    }
}


#pragma mark - JXCategoryListContentViewDelegate
/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}
@end
